import pathToRegexp from 'path-to-regexp';
import { evaluateDetail,evaluateLike,evaluateTwo } from './api';
import { Toast } from 'antd-mobile';
import { routerRedux } from 'dva/router';


export default {
    namespace: 'evaluateDetail',
    state: {
        list:[],
        evaluteEntity:{},
        hasMore:true,
        specId:"",
        goodsId:"",
        gevalScore:null,
        evaluateDetail:[],
        pageNo:1
    },
    reducers: {
        save(state, { data }) {
            return { ...state, ...data };
        },

        saveList(state, { data }) {

            if( data.pageNo !==undefined && data.pageNo == 1 ){

                return { ...state, ...data, hasMore: data.list.length == data.pageSize };
                //  yield put({ type: 'save', data: { ...result.ret, ...obj, hasMore: result.ret.list.length == result.pageSize } });
            }else {
                return { ...state, ...data, hasMore: data.list.length == data.pageSize, list:[...state.list, data.list ]  };
            }

        },


    },
    effects: {
        *evaluateDetailEFF({ obj }, { put, call }) {

            const result = yield call(evaluateDetail, obj);
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret });
            } else {

            }
        },

        *evaluateLikeEFF({ obj }, { put, call }) {

            const result = yield call(evaluateLike, obj);
            if (result.code == 200) {
                Toast.success(result.msg,1.5)
            } else {

            }
            return result
        },

        *evaluateTwoEFF({ obj }, { put, call }) {
            const result = yield call(evaluateTwo, obj);
            if (result.code == 200) {
                Toast.success(result.msg,1.5)
            } else {

            }
            return result
        },

        *addCartOrBuyEFF({ obj }, { put, call, select }) {

            const evaluateList = yield select((state)=>state.evaluateList);
            const result = yield call(addCartOrBuy, {  goodsId:evaluateList.goodsId, count:1, specId:evaluateList.specId , ...obj });
            if (result.code == 200) {
                if(obj.saveType==1){
                    yield put(routerRedux.push('/order'))
                }else {
                    Toast.success(result.msg, 1.5);
                }

            } else {

            }
        },

    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                //console.log(match)
                if(pathname.includes('/evaluateDetail/')){
                    const match = pathToRegexp('/evaluateDetail/:gevalId').exec(pathname);
                    dispatch({ type:'evaluateDetailEFF', obj:{ id:match[1] } });
                }
            });
        },
    },
};
